using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using ProjectSurvivor;
using QFramework;
using UnityEngine;

public class Global:Architecture<Global>//实现全局架构注册与依赖获取//集中注册系统（存档、金币升级、经验升级）。
{
    //model
    public static BindableProperty<int> HP = new BindableProperty<int>(3);//并与 PlayerPrefs 做持久化绑定。
    public static BindableProperty<int> MaxHP = new BindableProperty<int>(3);//可以绑定的属性,更新数据
    public static BindableProperty<int> Exp = new BindableProperty<int>(0);
    public static BindableProperty<float> BombDamage = new BindableProperty<float>(10);
    public static BindableProperty<float> BombPercent = new BindableProperty<float>(0.05f);
    public static BindableProperty<bool> BombUnlocked = new BindableProperty<bool>(false);
    public static BindableProperty<bool> SimpleSwordUnlocked = new BindableProperty<bool>(false);
    public static BindableProperty<bool> SimpleKnifeUnlocked = new BindableProperty<bool>(false);
    public static BindableProperty<bool> RotateSwordUnlocked = new BindableProperty<bool>(false);
    public static BindableProperty<bool> noRotateSwordUnlocked = new BindableProperty<bool>(false);
    public static BindableProperty<bool> BasketBallUnlocked = new BindableProperty<bool>(false);
    public static BindableProperty<int> Coin = new BindableProperty<int>(0);
    public static BindableProperty<int> Level = new BindableProperty<int>(1);
    public static BindableProperty<float> CurrentSeconds = new BindableProperty<float>(0);
    public static BindableProperty<float> speed = new BindableProperty<float>(1);
    public static BindableProperty<float> SimpleAbilityDamage = new BindableProperty<float>(Config.InitSimpleSwordDamage);
    public static BindableProperty<float> SimpleAbilityDuration = new BindableProperty<float>(Config.InitSimpleSwordDuration);
    public static BindableProperty<int> SimpleSwordCount = new BindableProperty<int>(Config.InitSimpleSwordCount);
    public static BindableProperty<float> SimpleSwordRange = new BindableProperty<float>(Config.InitSimpleSwordRange);
    public static BindableProperty<float> SimpleSwordSpeed = new BindableProperty<float>(1);
    public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.05f);
    public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.3f);
    public static BindableProperty<int> Coinlv = new BindableProperty<int>(1);
    public static BindableProperty<int> Coinlvsj = new BindableProperty<int>(5);
    public static BindableProperty<int> ExpAtklv = new BindableProperty<int>(1);
    public static BindableProperty<int> Explv = new BindableProperty<int>(1);
    public static BindableProperty<int> Swordlv = new BindableProperty<int>(1);
    public static BindableProperty<int> Bollspeed = new BindableProperty<int>(3);
    public static BindableProperty<float> BaoJI = new BindableProperty<float>(1);
    public static BindableProperty<bool> SuperKnife = new( false);
    public static BindableProperty<bool> SuperSword = new( false);
    public static BindableProperty<bool> SuperRotateSword = new( false);
    public static BindableProperty<bool> SuperBomb = new( false);
    public static BindableProperty<bool> SuperBasketBall = new( false);
    public static BindableProperty<int> tipe = new(-1);
    public static int type;
    public static void ResetData()//重置战斗相关的临时数据
    {
        HP.Value = MaxHP.Value;
        Exp.Value = 0;
        Level.Value = 1;
        CurrentSeconds.Value = 0;
        SimpleSwordUnlocked.Value = false;
        SimpleKnifeUnlocked.Value = false;
        RotateSwordUnlocked.Value = false;
        noRotateSwordUnlocked.Value = false;
        BasketBallUnlocked.Value = false;
        SimpleAbilityDamage.Value = Config.InitSimpleSwordDamage;
        SimpleAbilityDuration.Value = Config.InitSimpleSwordDuration;
        SimpleSwordRange.Value = Config.InitSimpleSwordRange;
        SimpleSwordCount.Value = Config.InitSimpleSwordCount;
        BombDamage.Value = 10;
        BombPercent.Value = 0.05f;
        Explv.Value = 0;
        ExpAtklv.Value = 0;
        SuperKnife.Value = false;
        SuperBomb.Value = false;
        SuperRotateSword.Value = false;
        SuperSword.Value = false;
        SuperBasketBall.Value = false;
    }
    public static int ExpToNextLevel()//计算升到下一级所需经验
    {
        return Level.Value * 5;
    }
    [RuntimeInitializeOnLoadMethod]//自动初始化资源系统与 UI 分辨率，并与 PlayerPrefs 做持久化绑定。
    public static void AutoInit()//从 PlayerPrefs 读取
    {
        ResKit.Init();
        UIKit.Root.SetResolution(1280,720,1);//设置 UI 分辨率
        HP.Value = MaxHP.Value;
        MaxHP.Value= PlayerPrefs.GetInt(nameof(MaxHP),3);
        Global.Coin.Value = PlayerPrefs.GetInt("coin", 0);
        Global.ExpPercent.Value = PlayerPrefs.GetFloat(nameof(ExpPercent), 0.4f);
        Global.CoinPercent.Value = PlayerPrefs.GetFloat(nameof(CoinPercent), 0.1f);//没有默认0.4f
        Global.Coinlv.Value = PlayerPrefs.GetInt(nameof(Coinlv), 1);
        Global.Coinlvsj.Value = PlayerPrefs.GetInt(nameof(Coinlvsj), 1);
        //string[] keys = GetAllKeys();
        // foreach (string key in keys)
        // {
        //     Debug.Log($"Key: {key}, Value: {PlayerPrefs.GetInt(key, -1)}");  // 这里假设都是int类型，可根据实际情况调整获取方式
        // }
        Global.Coin.RegisterWithInitValue(coin =>
        {
            PlayerPrefs.SetInt(nameof(coin),coin);
        });
        Global.ExpPercent.RegisterWithInitValue(expPercent =>
        {
            PlayerPrefs.SetFloat(nameof(expPercent), expPercent);
        });
        Global.CoinPercent.RegisterWithInitValue(coinPercent =>
        {
            PlayerPrefs.SetFloat(nameof(coinPercent),coinPercent);
        });
        Global.MaxHP.RegisterWithInitValue(maxHp =>
        {
            PlayerPrefs.SetFloat(nameof(maxHp),maxHp);
        });
        Global.Coinlv.RegisterWithInitValue(coinlv =>
        {
            PlayerPrefs.SetInt(nameof(Coinlv), coinlv);
        });
        Global.Coinlvsj.RegisterWithInitValue(coinlvsj =>
        {
            PlayerPrefs.SetInt(nameof(Coinlvsj),coinlvsj);
        });
        
    }
    //  public static string[] GetAllKeys()
    // {
    //     // 反射获取 PlayerPrefs 内部的键集合
    //     MethodInfo getKeysMethod = typeof(PlayerPrefs).GetMethod("GetKeys", BindingFlags.Static | BindingFlags.NonPublic);
    //     if (getKeysMethod == null)
    //     {
    //         Debug.LogError("PlayerPrefs.GetKeys 反射失败，可能 Unity 版本不兼容");
    //         return new string[0];
    //     }
    //     return (string[])getKeysMethod.Invoke(null, null);
    // }
    public static void GeneratePowerUp(UnityEngine.GameObject gameObject,bool treasureChest)//怪物死后物品随机
    {
        if (treasureChest)
        {
            PowerUpManage.Default.TreasureChest.Instantiate().Position(gameObject.Position()).Show();
        }
        var percent = Random.Range(0, 1f);
        if (percent < ExpPercent.Value)
        {
            PowerUpManage.Default.Exp.Instantiate().Position(gameObject.Position()).Show();
            return;
        }
        percent = Random.Range(0, 1f);
        if (percent < CoinPercent.Value)
        {
            PowerUpManage.Default.Coin.Instantiate().Position(gameObject.Position()).Show();
            return;
        }
        percent = Random.Range(0, 1f);
        if (percent < 0.1f)
        {
            PowerUpManage.Default.HP.Instantiate().Position(gameObject.Position()).Show();
        }
        if (BombUnlocked.Value && !UnityEngine.GameObject.FindObjectOfType<Bomb>())
        {
             percent = Random.Range(0, 1f);
            if (percent <BombPercent.Value)
            {
                PowerUpManage.Default.Bomb.Instantiate().Position(gameObject.Position()).Show();
            }
        }
       
        percent = Random.Range(0, 1f);
        if (percent < 0.1f)
        {
            PowerUpManage.Default.GetAllExp.Instantiate().Position(gameObject.Position()).Show();
        }
    }

    protected override void Init()
    {
       this.RegisterSystem(new SaveSystem());
       this.RegisterSystem(new CoinUpgradeSystem());
       this.RegisterSystem(new ExpUpgrdeSystem());
    }
}
